How to Display .pdf, .xlsx, .docx Files in a Cordova App

Description

Build-in handlers for displaying .pdf, .xlsx, .docx and other file types are usually not available on mobile devices. If your mobile app is built using Cordova, you can use the Document Handler Cordova plugin to display these file types.

Discussion

The Cordova Document Handler plugin allows you to view PDF, MS Office, iWork, RTF, TXT, and image files on a mobile device. If the Document Handler plugin is available, you can call the handleDocumentWithURL Document Handler plugin method to display a document. For example:

function showPDFDocument () {
    var docURL = 'http://downloads.alphasoftware.com/Cordova/documentation/alphaphonegapdoc.pdf';
    var wObj = {};
    wObj.message = "Loading document, please wait...";

    {dialog.object}.showWaitMessage('page',wObj);

    try {
        var onLoadSuccess = function () {
            {dialog.object}.hideWaitMessage();
            alert('Document loaded.');
        };
        var onLoadFail = function () {
            {dialog.object}.hideWaitMessage();
            alert('Document load failed.');
        };

        handleDocumentWithURL(onLoadSuccess, onLoadFail, docURL);

    } catch (e) {
        {dialog.object}.hideWaitMessage();
        alert(e.message);
    }
}

You can add the Document Handler plugin to your application in the Cordova Builder. Check the option for Document Handler under Third Party Plugins.

For more information on how to use the Document Handler plugin in your application, watch the following video: